package com.ssh.dao;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.mapping.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

//import com.ssh.model.People;
import com.ssh.model.Goods;
import com.ssh.model.TableGoods;

import com.ssh.dataobject.JqGridSearchTo;
import com.ssh.dataobject.JqGridSearchDetailTo;
import com.ssh.dataobject.QueryResult;

/**
 * A data access object (DAO) providing persistence and search support for Users
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.ssh.model.Users
 * @author MyEclipse Persistence Tools
 */

public class GoodsDAO extends HibernateDaoSupport implements IGoodsDAO {

	protected void initDao() {
		// do nothing
	}

	/*
	 * 添加Goods (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IGoodsDAO#save(com.ssh.model.Goods)
	 */
	public void save(TableGoods goods) {

		try {
			getHibernateTemplate().save(goods);
			System.out.println("CCC");
		} catch (RuntimeException re) {

			throw re;
		}
	}

	/*
	 * 删除Goods (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IGoodsDAO#delete(com.ssh.model.People)
	 */
	public void delete(TableGoods goods) {

		try {
			getHibernateTemplate().delete(goods);

		} catch (RuntimeException re) {

			throw re;
		}
	}

	// 更新用户
	public void update(TableGoods goods) {
		try {
			this.getHibernateTemplate().saveOrUpdate(goods);
		} catch (RuntimeException re) {

			throw re;
		}
	}

	/*
	 * 通过goodsid查询Goods (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IGoodsDAO#findById(java.lang.Integer)
	 */
	public TableGoods findById(java.lang.Integer id) {

		try {
			TableGoods instance = (TableGoods) getHibernateTemplate().get(
					TableGoods.class, id);
			return instance;
		} catch (RuntimeException re) {

			throw re;
		}
	}

	public List findByExample(TableGoods instance) {

		try {
			List results = getHibernateTemplate().findByExample(instance);

			return results;
		} catch (RuntimeException re) {

			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IPeopleDAO#findByProperty(java.lang.String,
	 * java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		List list = null;
		try {
			String queryString = "from TableGoods as model where model."
					+ propertyName + "= ?";
			System.out.println(queryString);
			list = getHibernateTemplate().find(queryString, value);
			System.out.println("hehe44444");
		} catch (Exception re) {
			re.printStackTrace();

		}
		return list;
	}

	/*
	 * 通过用户名查找Goods (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IGoodsDAO#findByGoodsname(java.lang.Object)
	 */

	// public List findByGoodsname(Object name) {
	// return findByProperty(NAME, name);
	// }
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IPeopleDAO#findByCode(java.lang.Object)
	 */
	public List findByCode(Object code) {
		return findByProperty(CODE, code);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IPeopleDAO#findByName(java.lang.Object)
	 */
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IPeopleDAO#findByInstrution(java.lang.Object)
	 */
	public List findByEpc(Object epc) {
		return findByProperty(EPC, epc);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IGoodsDAO#findByPerson(java.lang.Object)
	 */
	public List findByPeoplecode(Object peoplecode) {
		return findByProperty(PEOPLECODE, peoplecode);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IGoodsDAO#findByPicture(java.lang.Object)
	 */
	public List findByDetails(Object details) {
		return findByProperty(DETAILS, details);
	}

	public List<TableGoods> findAll() {

		try {
			String queryString = "from TableGoods";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {

			throw re;
		}

	}

	/*
	 * 查找所有用户 (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IGoodsDAO#findAll()
	 */
	public List<TableGoods> findAllPage(final int offset, final int pageSize) {

		try {
			final String queryString = "from TableGoods as g order by g.id asc";
			return (List) super.getHibernateTemplate().execute(
					new HibernateCallback() {
						// 重写其doInHibernate方法返回一个object对象，
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							// 创建query对象
							Query query = session.createQuery(queryString);
							// 返回其执行了分布方法的list
							return query.setFirstResult(offset)
									.setMaxResults(pageSize).list();

						}

					});

		} catch (RuntimeException re) {

			throw re;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IGoodsDAO#merge(com.ssh.model.Goods)
	 */
	public TableGoods merge(TableGoods detachedInstance) {

		try {
			TableGoods result = (TableGoods) getHibernateTemplate().merge(
					detachedInstance);
			return result;
		} catch (RuntimeException re) {

			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IGoodsDAO#attachDirty(com.ssh.model.Goods)
	 */
	public void attachDirty(TableGoods instance) {

		try {
			getHibernateTemplate().saveOrUpdate(instance);

		} catch (RuntimeException re) {

			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ssh.dao.IGoodsDAO#attachClean(com.ssh.model.Users)
	 */
	public void attachClean(TableGoods instance) {

		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);

		} catch (RuntimeException re) {

			throw re;
		}
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public TableGoods getGoods(Integer goodsid) {
		TableGoods goods = (TableGoods) getHibernateTemplate().load(
				TableGoods.class, goodsid);
		return goods;
	}

	@SuppressWarnings("unchecked")
	public TableGoods findGoods(String queryString) {
		List<TableGoods> list = getHibernateTemplate().find(queryString);
		Iterator iter = list.iterator();
		return (TableGoods) iter.next();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TableGoods> findGoodsListByHQL(String hql) {
		List<TableGoods> list = null;
		try {
			String queryString = "from TableGoods as model where " + hql
					+ "order by id asc";
			System.out.println(queryString);
			list = (List<TableGoods>) getHibernateTemplate().find(queryString);
			System.out.println("hehe44444");
		} catch (Exception re) {
			re.printStackTrace();

		}
		if (list == null) {
			System.out.println("list==null");
		} else {
			System.out.println("list.size()" + list.size());
		}
		return list;
	}

	// 获得用户数据总数
	@Override
	public int countGoods() {

		String hql = "select count(*) from TableGoods g";
		int count = 0;
		// Session session = null;
		// try {
		// session = getHibernateTemplate().getSession();
		// Query query = session.createQuery(hql);
		count = ((Number) getHibernateTemplate().find(hql).listIterator()
				.next()).intValue(); // 注意这里必须用Number
		// } catch (Exception e) {
		// e.printStackTrace();
		// } finally {
		// HibernateUtil.closeSession();
		// }
		System.out.println("count=" + count);
		return count;

	}

}
